package com.collectplus.express.parcel.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;

import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppLogic;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.ParcelSendBean;
import com.collectplus.express.model.PassportDetailBean;
import com.collectplus.express.parcel.BaseParcelFragment;
import com.collectplus.express.parcel.ParcelSendAdapter;

import java.util.ArrayList;

import droid.frame.utils.android.Log;

/**
 * 等待寄件
 *
 * @author kaka<br>
 *         2017-1-9下午12:00:48
 */
public class ParcelSendWaitFragment extends BaseParcelFragment {
    private ParcelSendAdapter mAdapter;
    private int pageNum = 1;

    @Override
    protected void findViewById() {
        setContentView(R.layout.parcel_send_list);
        super.findViewById();
        mAdapter = new ParcelSendAdapter(new ArrayList<ParcelSendBean>(), getActivityContext(), this);
        mListView.setAdapter(mAdapter);

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().getSendParceListNew(2, pageNum, MsgID.parcel_list_nosend);
            }
        });
        showLoadingDialog(null);
        AppHttp.getInstance().getSendParceListNew(2, pageNum, MsgID.parcel_list_nosend);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.passport_detail:
                final AppResult<PassportDetailBean> result1 = JSON.parser(msg.obj, PassportDetailBean.class);
                cancelLoadingDialog();
                if (result1.isSuccess()) {
                    PassportDetailBean passport = result1.getResult();
                    if (passport != null && isCurrentRequest) {
                        Log.i("lueng", getClass().getSimpleName() + "  ==" + isCurrentRequest);
                        AppLogic.getInstance().showPassportDialog(getActivityContext(), passport, null);
                    }
                } else {
                    showToast(result1.getMessage());
                }
                isCurrentRequest = false;
                return false;
            case MsgID.parcel_list_nosend:
                cancelLoadingDialog();
                onRefreshComplete();
                try {
                    final AppResult<ParcelSendBean> result = JSON.parseList(msg.obj, ParcelSendBean.class);// 解析可能出现问题
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (result.isSuccess()) {
                                parcels = result.getResultList();
                                // if (parcels != null && parcels.size() > 0) {
                                // mRefreshLayout.setVisibility(View.VISIBLE);
                                // mEmptyLayout.setVisibility(View.GONE);
                                mAdapter.getItems().clear();// 先清空，此版本暂时不做分页
                                mAdapter.appendItems(parcels, true);
                                // } else {// 判断是否刷新
                                // mRefreshLayout.setVisibility(View.GONE);
                                // mEmptyLayout.setVisibility(View.VISIBLE);
                                // }
                            } else {
                                // 未知原因是否需要显示
                                showToast(result.getMessage());
                            }
                        }
                    });
                    break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
        return false;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 102) {
            showLoadingDialog(null);
            AppHttp.getInstance().getSendParceListNew(2, pageNum, MsgID.parcel_list_nosend);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}